import { APIGoodsDetail, APIAddCart } from "./fetch.js";

import { queryString, getCookie } from './tools.js';

// $('#head').load('../../layout/head.html');
// $('#foot').load('../../layout/foot.html');

layui.use(['layer'], function () {

  var gid = queryString('gid');

  APIGoodsDetail({
    gid
  }).then(({ data}) => {
    $('#neirong_mid_left').html(`
        <div id="box_img">
            <img src="${data.img}" alt="">
            </div>
            <div id="box_bottom">
                <ul>
                    <li><img src="https://img.suofeiya.com.cn/images/202007/817953ae324f9e6dd6f7dda446a098e2.jpg"
                        alt=""></li>
                    <li><img src="https://img.suofeiya.com.cn/images/202007/167d677e623b49c8b7e709b030f0aff1.jpg"
                        alt=""></li>
                    <li><img src="https://img.suofeiya.com.cn/images/202007/4bb56f376b05ea39d35c5020c7862bb6.jpg"
                        alt=""></li>
                    <li><img src="https://img.suofeiya.com.cn/images/202007/2982406ddbb0d6d7ee3d8651bbccdfb3.jpg"
                        alt=""></li>
                </ul>
            </div>
        </div>
        `
    
        )
    $('#neirong_mid_right').html(
            `
            <h2>${data.gname}</h2>
            <p id="p11">${data.desc}；<span id="span_1">详情请咨询客服</span></p>
            <p id="p22">&nbsp&nbsp&nbsp&nbsp定制金：<span>￥</span><span id="jiage">${data.price}</span></p>
            <div id="bianma">
                <span>编码 ： <p>GXGK-UN19</p></span>

            </div>
            <div id="guige">
                <span>规格 ：</span>
                <a href="#">&nbsp&nbsp&nbsp&nbsp定制诚意金（具体价格以设计方案为准，详情咨询客服）</a>
            </div>
            <div id="shuliang">
                <span id="spa1">数量：</span>
                <span id="spa_jian">-</span>
                <input type="text" name="" id="inp_1" value="1">
                <span id="spa_jia">+</span>
                <span id="spa2">库存:&nbsp&nbsp100</span>
                <span id="spa3"><i id="i2"
                        class="layui-icon layui-icon-heart-fill"></i>&nbsp喜欢&nbsp&nbsp45896</span>
                <div id="xuanxiang">
                    <a id="xiadan" >立即购买</a>
                    <a id="jiagou" >加入购物车</a>
                </div>
                <ul>
                    <li>
                        <img src="https://www.suofeiya.com/Www/Public_1/images/promise_1.png" alt="">
                        <span>正品保证</span>
                    </li>
                    <li>
                        <img src="https://www.suofeiya.com/Www/Public_1/images/promise_2.png" alt="">
                        <span>环保板材</span>
                    </li>
                    <li>
                        <img src="https://www.suofeiya.com/Www/Public_1/images/promise_3.png" alt="">
                        <span>免费服务</span>
                    </li>
                </ul>

            </div>
            `
        )
    // $('.layui-tab-content').html(`
    //     <div class="layui-tab-item layui-show">
    //         <img src="https://img.suofeiya.com.cn/images/202008/db20b64c-62d8-e51f-5d9f-c806871ef73d.jpg" alt="">
    //         <img src="https://img.suofeiya.com.cn/images/202008/db20b64c-62d8-e51f-5d9f-c806871ef73d.jpg" alt="">
    //         <img src="https://img.suofeiya.com.cn/images/202008/db20b64c-62d8-e51f-5d9f-c806871ef73d.jpg" alt="">
    //         <img src="https://img.suofeiya.com.cn/images/202008/db20b64c-62d8-e51f-5d9f-c806871ef73d.jpg" alt="">
    //         <img src="https://img.suofeiya.com.cn/images/202008/db20b64c-62d8-e51f-5d9f-c806871ef73d.jpg" alt="">
    //         <img src="https://img.suofeiya.com.cn/images/202008/db20b64c-62d8-e51f-5d9f-c806871ef73d.jpg" alt="">
    //         <img src="https://img.suofeiya.com.cn/images/202008/db20b64c-62d8-e51f-5d9f-c806871ef73d.jpg" alt="">
    //         <img src="https://img.suofeiya.com.cn/images/202008/db20b64c-62d8-e51f-5d9f-c806871ef73d.jpg" alt="">
    //         <img src="https://img.suofeiya.com.cn/images/202008/db20b64c-62d8-e51f-5d9f-c806871ef73d.jpg" alt="">
    //         <img src="https://img.suofeiya.com.cn/images/202008/db20b64c-62d8-e51f-5d9f-c806871ef73d.jpg" alt="">
    //     </div>
    //     <div class="layui-tab-item">内容2</div>
    //     <div class="layui-tab-item">内容3</div>
    //     `
    
    //     )
  })
  var coo = 0;
  $(document).on('click','#spa_jia',function(){
      coo++
    $('#inp_1').val(coo)
    var aoo = $('#inp_1').text()
    // $(document).on('click','#spa_jian',function(){
      
    //   $('#inp_1').val(--aoo)
    
    
    // })
  })

  $(document).on('click', '#jiagou', function() {
    var username = getCookie('login_user');
    if(!username) {
      sessionStorage.loginBack = location.href;
      location.href = "../../html/login.html";
    }

    APIAddCart({
      gid,
      username,
      img: $('#box_img img').attr('src'),
      gname: $('#neirong_mid_right h2').text(),
      price: $('#jiage').text(),
      count: $('#inp_1').val()
    }).then(({code, msg}) => {
      if(code) {
        console.log(1)
        location.href = "../../html/cart.html"
      } else {
        console.log(2)
        layer.msg(msg)
        // location.href = "../../html/cart.html";
      }
    })

  })
  

})

